home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-15 | 8.4 KB | 505 lines | [TEXT/KAHL] |
- /*
- * Main.c
- *
- * Robert Dierkes, November 11, 1993
- *
- * Change History:
- *
- * 11/93 ??? New
- * 4/96 bob Updated #includes to support changed GX Library names.
- * Added the copyright info.
- *
- *
- * © Apple Computer, Inc. 1990 - 1996 All rights reserved
- *
- */
-
-
- #undef MAC_HEADERS
-
-
- /*------------------*/
- /* Include Files */
- /*------------------*/
- #ifndef MAC_HEADERS
- #include <Memory.h>
- #include <Fonts.h>
- #include <Menus.h>
- #include <OSEvents.h>
- #include <Dialogs.h>
- #include <ToolUtils.h>
- #include <Desk.h>
- #include <GestaltEqu.h>
- #endif MAC_HEADERS
-
- #include <GXEnvironment.h>
- #include "GraphicsLibraries.h"
- #include <GXErrors.h>
-
- #include "ResourceIDs.h"
- #include "Window.h"
- #include "ShapeAction.h"
- #include "AutoMouse.h"
-
- #include "Main.h"
-
-
- /*----------------------*/
- /* Global Declarations */
- /*----------------------*/
- gxGraphicsClient gClient;
- Boolean gQuitApp;
- Handle ghMainMenu;
- Rect gDragRect;
- WindowPtr gWindow;
-
-
- /*------------------------------*/
- /* External Declarations */
- /*------------------------------*/
- extern GlobalStructure globals;
-
-
- Boolean
- InitApp (void)
- {
- long response;
- gxGraphicsError grfxError;
-
- #ifndef linkedIn
- if (Gestalt (gestaltGraphicsVersion, &response)) {
- DebugStr ("\pQuickDraw GX is not installed");
- return false;
- }
- #endif
-
- if ((gClient = GXNewGraphicsClient (nil, kGraphicsHeapSize, 0)) == 0) {
- DebugStr ("\pGXNewGraphicsClient failed");
- return false;
- }
-
- /* Initialize Skia */
- GXEnterGraphics ();
-
- GXSetValidation (gxNoValidation);
- SetGraphicsLibraryErrors ();
- SetGraphicsLibraryNotices();
- InitCommonColors ();
-
- MaxApplZone();
- MoreMasters();
- MoreMasters();
-
- InitGraf (&qd.thePort);
- InitFonts ();
- FlushEvents (everyEvent, 0);
- InitWindows ();
- InitMenus ();
- InitDialogs (nil);
- InitCursor ();
-
- return true;
-
- } /* InitApp */
-
-
- void
- ExitApp (void)
- {
- gxViewPort parent;
-
- if (gWindow)
- {
- if (parent = GXGetWindowViewPort (gWindow))
- GXDisposeViewPort (parent);
- DisposeDialog (gWindow);
- }
-
- DisposeCommonColors ();
- GXExitGraphics ();
- if (gClient)
- GXDisposeGraphicsClient (gClient);
-
- } /* ExitApp */
-
-
- void
- InitializeMenus (void)
- {
- ghMainMenu = GetNewMBar (menuBarRsrcID);
- SetMenuBar (ghMainMenu);
- AddResMenu (GetMHandle (appleMenuRsrcID), 'DRVR');
- DrawMenuBar ();
-
- } /* InitializeMenus */
-
-
- WindowPtr
- InitializeWindow (void)
- {
- WindowPtr pWindow;
- Rect bounds;
- short totalMargin;
- Point newPosition;
- GDHandle hGDevice;
-
- if ((pWindow = GetNewDialog (dialogRsrcID, nil, kWindowOnTop)) == nil)
- return (nil);
-
- SetPort (pWindow);
- SetDefaultViewPort (GXNewWindowViewPort (pWindow));
-
- /* Find the deepest screen */
- SetRect (&bounds, -32767, -32767, 32767, 32767);
- hGDevice = GetMaxDevice (&bounds);
- bounds = (**hGDevice).gdRect;
-
- totalMargin = (bounds.right - bounds.left) - (pWindow->portRect.right - pWindow->portRect.left);
- newPosition.h = bounds.left + (totalMargin >> 1);
-
- totalMargin = (bounds.bottom - bounds.top) - (pWindow->portRect.bottom - pWindow->portRect.top - kWindowTitleHeight);
- newPosition.v = bounds.top + ((totalMargin + ((hGDevice == GetMainDevice ()) ? GetMBarHeight () : 0)) >> 1);
-
- MoveWindow (pWindow, newPosition.h, newPosition.v, true);
- ShowWindow (pWindow);
-
- gDragRect = qd.screenBits.bounds;
- return (pWindow);
-
- } /* InitializeWindow */
-
-
- void
- DoContentClick (EventRecord *pEvent, WindowPtr pWindow)
- {
- if (pWindow == nil)
- return;
-
- if (FrontWindow () == pWindow)
- {
- if (pWindow == gWindow)
- DoHitTestContent (pEvent, pWindow);
- }
- else
- {
- SetPort (pWindow);
- SelectWindow (pWindow);
- }
-
- } /* DoContentClick */
-
-
- void
- DoMenuCommand (menuResult)
- long menuResult;
- {
- short menuID,
- itemNumber;
-
- if (! menuResult)
- return;
-
- menuID = HiWord (menuResult);
- itemNumber = LoWord (menuResult);
-
- switch (menuID)
- {
- case appleMenuRsrcID:
- switch (itemNumber)
- {
- case itemAbout:
- Alert (aboutRsrcID, nil);
- break;
-
- default:
- // DoDeskAcc (appleMenuRsrcID, itemNumber);
- SysBeep (1);
- break;
- }
- break;
-
- case fileMenuRsrcID:
- switch (itemNumber)
- {
- case itemQuit:
- gQuitApp = true;
- break;
-
- default:
- SysBeep (1);
- break;
- }
- break;
-
- case metricsMenuRsrcID:
- switch (itemNumber)
- {
- case itemShowControlPoints:
- case itemShowLocalBounds:
- ToggleMetricsMenuItem (itemNumber, gWindow);
- break;
-
- case itemAutoMouse:
- ToggleAutoMouse ();
- break;
-
- default:
- SysBeep (1);
- break;
- }
- break;
-
- default:
- break;
- } /* switch (menuID) */
-
- HiliteMenu (kHiliteAllMenus);
-
- } /* DoMenuCommand */
-
-
- void
- DoGrowBox (EventRecord *pEvent, WindowPtr pWindow)
- {
- long newSize;
- Rect newRect,
- oldRect;
-
- oldRect = newRect = pWindow->portRect;
-
- if (newSize = GrowWindow (pWindow, pEvent->where, &qd.screenBits.bounds))
- {
- newRect.right = newRect.left + (short) newSize;
- newRect.bottom = newRect.top + (short) (newSize >> 16);
- SizeWindow (pWindow,
- newRect.right - newRect.left,
- newRect.bottom - newRect.top,
- true);
- InvalRect (&oldRect);
- }
-
- } /* DoGrowBox */
-
-
- void
- DoZoomBox (EventRecord *pEvent, WindowPtr pWindow, short windowPart)
- {
- if (TrackBox (pWindow, pEvent->where, windowPart))
- {
- ZoomWindow (pWindow, windowPart, true);
- InvalRect (&pWindow->portRect);
- }
-
- } /* DoZoomBox */
-
-
- void
- DoNullEvent (EventRecord *pEvent)
- {
- IdleHitTestWindow (gWindow, pEvent);
- MoveAutoMouse ();
-
- } /* DoNullEvent */
-
-
- void
- DoMouseDown (EventRecord *pEvent)
- {
- short windowPart;
- WindowPtr whichWindow;
-
- windowPart = FindWindow (pEvent->where, &whichWindow);
-
- switch (windowPart)
- {
- case inDesk:
- break;
-
- case inMenuBar:
- DoMenuCommand (MenuSelect (pEvent->where));
- break;
-
- case inSysWindow:
- SystemClick (pEvent, whichWindow);
- break;
-
- case inContent:
- DoContentClick (pEvent, whichWindow);
- break;
-
- case inDrag:
- DragWindow (whichWindow, pEvent->where, &gDragRect);
- break;
-
- case inGrow:
- DoGrowBox (pEvent, whichWindow);
- break;
-
- case inGoAway:
- if (TrackGoAway (whichWindow, pEvent->where))
- {
- if (whichWindow == gWindow)
- gQuitApp = true;
- else
- DebugStr ("\pDoMouseDown: Clicked in window of unknown close box");
- }
- break;
-
- case inZoomIn:
- case inZoomOut:
- DoZoomBox (pEvent, whichWindow, windowPart);
- break;
-
- default:
- break;
- } /* switch */
-
- } /* DoMouseDown */
-
-
- void
- DoKeyStroke (EventRecord *pEvent)
- {
- char charCode;
-
- charCode = pEvent->message & charCodeMask;
-
- if (pEvent->modifiers & btnState)
- {
- /*--------------*/
- /* Button is UP */
- /*--------------*/
- if (pEvent->modifiers & cmdKey)
- {
- /*-------------------*/
- /* Command key x100 */
- /*-------------------*/
- DoMenuCommand (MenuKey (charCode));
- }
- }
-
- } /* DoKeyStroke */
-
-
- void
- DoUpdate (EventRecord *pEvent)
- {
- GrafPtr savedPort;
- WindowPtr pUpdateWindow;
-
- GetPort (&savedPort);
-
- pUpdateWindow = (WindowPtr) pEvent->message;
-
- SetPort (pUpdateWindow);
- BeginUpdate (pUpdateWindow);
-
- EraseRgn (pUpdateWindow->visRgn);
-
- if (pUpdateWindow == gWindow)
- UpdateHitTestWindow (gWindow);
-
- EndUpdate (pUpdateWindow);
- SetPort (savedPort);
-
- } /* DoUpdate */
-
-
- void
- DoActivate (EventRecord *pEvent)
- {
- WindowPtr pActiveWindow;
-
- /*--------------------------------------*/
- /* Get the window to de/activate */
- /* and its kind from the event message */
- /*--------------------------------------*/
- pActiveWindow = (WindowPtr) pEvent->message;
-
- SetPort (pActiveWindow);
-
- /*-----------------*/
- /* Activate window */
- /*-----------------*/
- if (pEvent->modifiers & activeFlag)
- {
- /* Enable/Disable items */
- }
- else
- /*-------------------*/
- /* Deactivate window */
- /*-------------------*/
- {
- /* Enable/Disable items */
- }
-
- } /* DoActivate */
-
-
- void
- DoEvent (void)
- {
- EventRecord theEvent;
-
- SystemTask (); /* Handle desk accessories */
-
- GetNextEvent (everyEvent, &theEvent);
-
- switch (theEvent.what)
- {
- case nullEvent:
- DoNullEvent (&theEvent);
- break;
-
- case mouseDown:
- DoMouseDown (&theEvent);
- break;
-
- case keyDown:
- case autoKey:
- DoKeyStroke (&theEvent);
- break;
-
- case updateEvt:
- DoUpdate (&theEvent);
- break;
-
- case activateEvt:
- DoActivate (&theEvent);
- break;
-
- case mouseUp:
- case keyUp:
- case diskEvt:
- case networkEvt:
- case driverEvt:
- default:
- break;
- } /* switch (theEvent.what) */
-
- } /* DoEvent */
-
-
- main (void)
- {
- gQuitApp = false;
- if (InitApp ())
- {
- InitializeMenus ();
- if (gWindow = InitializeWindow ())
- {
- if (InitializeHitTesting (gWindow))
- {
- InitializeAutoMouse (globals.boxes);
-
- while (! gQuitApp)
- DoEvent ();
-
- CleanupHitTesting ();
- }
- }
- ExitApp ();
- }
-
- } /* main */
-